var satellite = new AMap.TileLayer.Satellite();
var roadNet = new AMap.TileLayer.RoadNet();
var mapObj = new AMap.Map('mapContainer', {
    resizeEnable: true, //
    //mapStyle: "amap://styles/light",
    center: new AMap.LngLat(113.535207, 34.797802),
    zoom: 5
});
var cluster;
var marker_group = [];

$(function () {
    getDeviceByStatus();
    loadMap();
})

$("#param_id").focus(function () {
    $("#listItem").show();
});

function findDevice() {
    let param = $("#param_id").val();
    if (!param) {
        $('#listItem').empty();
        return;
    }
    getJson("/device/getDeviceLikeParam", {"param": param}, 'GET', function (data) {
        $('#listItem').empty();
        let str = '';
        for (let i = 0; i < data.length; i++) {
            str += '<a href="#" class="list-group-item list-group-item-action" onclick="showGisSelect(\'' + data[i].id + '\',\'' + data[i].name + '\')">' + data[i].name + '</a>';
        }
        $('#listItem').slideDown(0);
        $("#listItem").append(str);
        str = '';
    })
}

function showGisSelect(deviceId, deviceName) {
    $("#listItem").hide();
    $('#param_id').val(deviceName);
    showDeviceInfo(deviceId);
}

/*展示设备信息toast*/
function showDeviceInfo(deviceId) {
    getJson("/device/getGISDeviceInfo", {"id": deviceId}, 'GET', function (result) {
        if (result.x && result.y) {
            var nar = new Array(result.x, result.y);
            mapObj.setCenter(nar);
        }
        $('#device_Name').html(result.name);
        let i;
        switch (result.status) {
            case 1:
                i = '<i class="iconfont icon-gongzuo mr-1 text-powerOn"></i>' + '运行中';
                break;
            case 2:
                i = '<i class="iconfont icon-gongzuo mr-1 text-standby"></i>' + '空闲';
                break;
            case 3:
                i = '<i class="iconfont icon-gongzuo mr-1 text-powerOff"></i>' + '离线';
                break;
            case 4:
                i = '<i class="iconfont icon-gongzuo mr-1 text-fault"></i>' + '报警';
                break;
        }
        $('#device_Status').empty();
        $('#device_Status').append(i);
        $('#device_sn').html(result.sn);
        $('#today_PTime').html(Math.round(result.productionTime * 100) / 100 + "h");
        $('#today_EC').html(result.todayEC + 'kWh');
        $('#today_EF').html(result.todayEF + '元');
        $('#device_Address').empty();
        $('#device_Address').append(
            '<i class="iconfont icon-guanjidingwei"></i> ' + result.province + ' / ' + result.city + ' / ' + result.company + ' / ' + result.name + ''
        );
        $('#device_DetailedLike').empty();
        $('#device_DetailedLike').append('<a href="##" onclick="showInfo(' + deviceId + ')">查看详细信息&gt;</a>');
        $('.toast').toast('show');
    })

}

function showInfo(e) {
    sessionStorage.setItem("deviceID", e);
    var iframeDeviceList = $('#LAY_app_body iframe[src="/pages/deviceList"]', window.parent.document);
    parent.layui.index.openTabsPage("/pages/deviceList", "设备列表");
    if (iframeDeviceList.length > 0) {//设备列表标签已打开
        var showDeviceId = iframeDeviceList[0].contentWindow.deviceId;//当前展示的设备id
        if (Number(e) != Number(showDeviceId)) {//当前展示与这次点击的设备不一致
            iframeDeviceList[0].contentWindow.loadDeviceDataByDeviceId();
        }
    }
}

/*查询各个状态下的设备信息*/
function getDeviceByStatus() {
    getJson("/device/getDeviceByStatus", null, 'GET', function (result) {
        if (result.code == 200) {
            let jobs = result.payload.data.jobs;
            let standby = result.payload.data.standby;
            let down = result.payload.data.down;
            let malfunction = result.payload.data.malfunction;
            if (jobs.length > 0) {
                $('#jobs-id').html(jobs.length);
                let as = '';
                for (let i = 0; i < jobs.length; i++) {
                    as += '<a class="dropdown-item" href="#" onclick="showDeviceInfo(' + jobs[i].id + ')">' + jobs[i].name + '</a>'
                }
                let divs = '<div class="dropdown-menu">' +
                    as +
                    '</div>';

                $('#status1-d-list').append(divs);
            }
            if (standby.length > 0) {
                $('#standby-id').html(standby.length);
                let as = '';
                for (let i = 0; i < standby.length; i++) {
                    as += '<a class="dropdown-item" href="#" onclick="showDeviceInfo(' + standby[i].id + ')">' + standby[i].name + '</a>'
                }
                let divs = '<div class="dropdown-menu">' +
                    as +
                    '</div>';
                $('#status2-d-list').append(divs);
            }
            if (down.length > 0) {
                $('#down-id').html(down.length);
                let as = '';
                for (let i = 0; i < down.length; i++) {
                    as += '<a class="dropdown-item" href="#" onclick="showDeviceInfo(' + down[i].id + ')">' + down[i].name + '</a>'
                }
                let divs = '<div class="dropdown-menu">' +
                    as +
                    '</div>';
                $('#status3-d-list').append(divs);
            }
            if (malfunction.length > 0) {
                $('#malfunction-id').html(malfunction.length);
                let as = '';
                for (let i = 0; i < malfunction.length; i++) {
                    as += '<a class="dropdown-item" href="#" onclick="showDeviceInfo(' + malfunction[i].id + ')">' + malfunction[i].name + '</a>'
                }
                let divs = '<div class="dropdown-menu">' +
                    as +
                    '</div>';
                $('#status4-d-list').append(divs);
            }
        } else {
            myAlert("提示", result.message())
        }

    })
}

function loadMap() {
    getJson("/device/getProductionEquipment", null, 'GET', function (result) {
        if (result.code == 200) {
            let array = new Array();
            for (let j = 0; j < result.payload.data.length; j++) {
                let data = result.payload.data[j];
                let obj = new Object();
                if (data.position) {
                    obj.position = data.position.split(",");
                } else {
                    continue;
                }
                obj.title = data.name;
                obj.status = parseInt(data.status);
                obj.did = data.id;
                array.push(obj);
            }
            //地图添加标记点
            var marker_arr = array;
            for (var i = 0; i < marker_arr.length; i++) {
                var icon = '';
                switch (marker_arr[i].status) {
                    case 1:
                        icon = '../customize/images/icon/gis-icon1.png';
                        break;
                    case 2:
                        icon = '../customize/images/icon/gis-icon2.png';
                        break;
                    case 3:
                        icon = '../customize/images/icon/gis-icon3.png';
                        break;
                    case 4:
                        icon = '../customize/images/icon/gis-icon4.png';
                        break;
                    default:
                        icon = '../customize/images/icon/gis-icon1.png';
                        break;
                }
                var marker = new AMap.Marker({
                    position: marker_arr[i].position,   //基点位置
                    offset: {x: 0, y: 0}, //相对于基点的位置
                    map: mapObj,
                    anchor: 'center',
                    label: {
                        content: marker_arr[i].title,
                        offset: new AMap.Pixel(0, 29),
                        direction: 'center'
                    },
                    icon: icon
                });
                let did = array[i].did;
                AMap.event.addListener(marker, "click", function () {
                    showDeviceInfo(did);
                })
                marker_group.push(marker);
            }
            addCluster();
        } else {
            myAlert("提示", result.message);
        }
        /*设置地图自适应显示*/
        mapObj.setFitView(marker_group);
    })

}

function addCluster() {
    if (cluster) {
        cluster.setMap(null);
    } else {
        mapObj.plugin(["AMap.MarkerClusterer"], function () {
            cluster = new AMap.MarkerClusterer(mapObj, marker_group);
        });
    }
}